package com.yogee.topseller.activity.my;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.R;
import com.yogee.topseller.adapter.ChoseCardAdapter;
import com.yogee.topseller.bean.BankInformationBean;
import com.yogee.topseller.bean.SelectBankByCaseBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ChoseCardActivity extends HttpActivity {
    @BindView(R.id.sure)
    TextView sure;
    @BindView(R.id.back)
    ImageView back;
    private List<SelectBankByCaseBean.BankListBean> bankListBeen = new ArrayList<>();
    @BindView(R.id.chose_recycler)
    RecyclerView choseRecycler;
    private ChoseCardAdapter adapter;
    String cardId = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_chose_card;
    }

    @Override
    protected void initView() {
        adapter = new ChoseCardAdapter(this);
        choseRecycler.setLayoutManager(new LinearLayoutManager(this));
        choseRecycler.setAdapter(adapter);
        selectBankByCase(AppUtil.getUserId(this));

        adapter.setClickItemListener(new ChoseCardAdapter.clickItemListener() {
            @Override
            public void clickItem(int position, String isDefault, String id) {
                if (isDefault.equals("0")) {
                    for (int i = 0; i < bankListBeen.size(); i++) {
                        bankListBeen.get(i).setIfDefault("0");
                    }
                    bankListBeen.get(position).setIfDefault("1");
                    adapter.notifyDataSetChanged();
                }
                cardId = id;
            }
        });
    }

    public void selectBankByCase(String userId) {
        HttpManager.getInstance().selectBankByCase(userId).compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<SelectBankByCaseBean>() {
            @Override
            public void onNext(SelectBankByCaseBean o) {
                ChoseCardActivity.this.loadingFinished();
                bankListBeen = o.getBankList();
                adapter.setBankListBeen(bankListBeen);
//                for (int i = 0; i < bankListBeen.size(); i++) {
//                    if (bankListBeen.get(i).getIfDefault().equals("1")) {
//                        CardId = bankListBeen.get(i).getId();
//                    }
//                }
            }
        }, this));
    }

    public void bankInformation(String userId, String cardId) {
        HttpManager.getInstance().bankInformation(userId, cardId).compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<BankInformationBean>() {
            @Override
            public void onNext(BankInformationBean o) {
                ChoseCardActivity.this.loadingFinished();
                if ("1".equals(o.getResult())) {
                    Intent intent = new Intent();
                    intent.putExtra("cardId", o.getId());
                    setResult(111, intent);
                    finish();
                }
            }
        }, this));
    }

    @OnClick({R.id.back, R.id.sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                Intent intent = new Intent();
                intent.putExtra("cardId", cardId);
                setResult(111, intent);
                finish();
                break;
            case R.id.sure:
                bankInformation(AppUtil.getUserId(ChoseCardActivity.this), cardId);
                break;
        }
    }
}
